function varargout = leds(varargin)
% LEDS MATLAB code for leds.fig
%      LEDS, by itself, creates a new LEDS or raises the existing
%      singleton*.
%
%      H = LEDS returns the handle to a new LEDS or the handle to
%      the existing singleton*.
%
%      LEDS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in LEDS.M with the given input arguments.
%
%      LEDS('Property','Value',...) creates a new LEDS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before leds_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to leds_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help leds

% Last Modified by GUIDE v2.5 11-Nov-2014 13:40:24

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @leds_OpeningFcn, ...
                   'gui_OutputFcn',  @leds_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before leds is made visible.
function leds_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to leds (see VARARGIN)

% Choose default command line output for leds
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes leds wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = leds_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
varargout{1} = handles.output;
% Get default command line output from handles structure



% --- Executes on button press in led1.
function led1_Callback(hObject, eventdata, handles)
    
% hObject    handle to led1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global Estado;
    fprintf(Estado, '%s', 'a');

% --- Executes on button press in led2.
function led2_Callback(hObject, eventdata, handles)
    
% hObject    handle to led2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global Estado;
    fprintf(Estado, '%s', 'b');


% --- Executes on button press in led3.
function led3_Callback(hObject, eventdata, handles)
  
% hObject    handle to led3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
  global Estado;
    fprintf(Estado, '%s', 'c');

% --- Executes on button press in led4.
function led4_Callback(hObject, eventdata, handles)
    
% hObject    handle to led4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global Estado;
    fprintf(Estado, '%s', 'd');

% --- Executes on button press in desconectar.
function desconectar_Callback(hObject, eventdata, handles)
  

% hObject    handle to desconectar (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
  global Estado;
    fclose(Estado);
    delete(Estado)
    clear Estado
    delete(instrfind({'Port'},{'COM13'}))

% --- Executes on button press in Conectar.
function Conectar_Callback(hObject, eventdata, handles)
   
% hObject    handle to Conectar (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 global Estado;
    Estado=serial('COM13');
    set(Estado,'BaudRate',9600);
    set(Estado,'DataBits',8);
    set(Estado,'Parity','none');
     set(Estado,'Terminator','LF');
    set(Estado,'StopBits',1);
    set(Estado,'FlowControl','none');
    fopen(Estado);


% --- Executes on button press in todo.
function todo_Callback(hObject, eventdata, handles)
% hObject    handle to todo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global Estado;
    fprintf(Estado, '%s', 'h');


% --- Executes on button press in RECIBE.
function RECIBE_Callback(hObject, eventdata, handles)
% hObject    handle to RECIBE (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global Estado;
Estado.BytesAvailable; 

pause(1);
A = fscanf(Estado,'%c',1);
 pause(1);

if eq(A,'A')
     
   fprintf(Estado, '%s', 'b');  
    pause(1);
  
   
   interv=1000;
passo=1;
t=1;

while(t<interv)

y=[200,1000];
x=[300,50];
plot(x,y);
axis([0,interv,0,1023]);
grid
t=t+passo;
drawnow;
end
   
   

else
    if eq(A,'B')
          pause(1);
   fprintf(Estado, '%s', 'a');  
   
   
   
      interv=1000;
passo=1;
t=1;

while(t<interv)

y=[0,100];
x=[0,50];
plot(x,y);
axis([0,interv,0,1023]);
grid
t=t+passo;
drawnow;
end
    end
end

    
